home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’95 / NetFractal™ / Fractal 8 source / InPoint.cpp < prev    next >
Encoding:
Text File  |  1995-06-24  |  1.5 KB  |  108 lines  |  [TEXT/MPCC]

  1. // InPoint.cpp
  2.  
  3. #include "InPoint.h"
  4.  
  5. #define TIMEOUT 2000
  6.  
  7.  
  8.  
  9. InPoint::InPoint(
  10.     const char *protocol,
  11.     const char *address,
  12.     short port)
  13. {
  14.     OSErr err = Open(protocol);
  15.     DNSAddress inAddress;
  16.     InetAddress outAddress;
  17.     TBind inBind = { { 0, sizeof(short)+strlen(address)-1, (UInt8*) &inAddress }, 0};
  18.     TBind outBind = { { sizeof(InetAddress), 0, (UInt8*) &fHisAddress }, 0};
  19.  
  20.  
  21.     if (err)
  22.         throw err;
  23.  
  24.     err = Bind(port);
  25.     if (err)
  26.         throw err;
  27.  
  28.     inAddress.fAddressType = kOTGenericName;
  29.     strcpy(inAddress.fName, address);
  30.  
  31.     err = OTResolveAddress(fEndpoint, &inBind, &outBind, TIMEOUT);
  32.     if (err)
  33.         throw err;
  34.     fHisAddressSize = outBind.addr.len;
  35.     
  36.     
  37.     // make it asynchronous and set up to receive
  38.     
  39.     OTInstallNotifier(fEndpoint, &Notifier, this);
  40.     
  41. }
  42.  
  43.  
  44. InPoint::~InPoint()
  45. {
  46. }
  47.  
  48.  
  49.  
  50. pascal void
  51. InPoint::Notifier(
  52.     void* contextPtr,
  53.     OTEventCode code, 
  54.     OTResult result,
  55.     void* cookie)
  56.  
  57.     // *** Called at deferred task time. ***
  58.  
  59. {
  60.     InPoint* thisPoint = (InPoint*) contextPtr;
  61.     thisPoint->HandleNotify(code, result, cookie);
  62. }
  63.  
  64.  
  65. void
  66. InPoint::HandleNotify(
  67.     OTEventCode code, 
  68.     OTResult result,
  69.     void* cookie)
  70.  
  71.     // *** Called at deferred task time. ***
  72.  
  73. {
  74.     switch (code) {
  75.         case T_DATA:
  76.             GetData();
  77.             break;
  78.         
  79.         
  80.  
  81.     }
  82. }
  83.  
  84.  
  85. void
  86. InPoint::GetData()
  87.  
  88. {
  89.     while (1) {
  90.         TUnitData uData = {
  91.             { 0, 0, NULL },
  92.             { 0, 0, NULL },
  93.             { 0, sizeof(packetBuffer), (UInt8*) &fPacket },
  94.         };
  95.         OTFlags flags;
  96.         
  97.         OSStatus theErr = OTRcvUData(fEndpoint, &uData, &flags);
  98.         if (theErr == kOTNoDataErr)
  99.             break;
  100.         if (theErr != noErr)
  101.             continue;
  102.  
  103.         // do something with data
  104.  
  105.  
  106.     }    
  107.  
  108. }